<template>
    <el-cascader
      :options="options"
      v-model="selectedOptions"
      @change="handleChange"
      :disabled="disabled"
      size="small">
    </el-cascader>
</template>


<script>
  import {list2tree} from '@/utils/treeUtils';
  import {getAdi} from '@/api/finInst'
  import {querySql} from '@/api/sqlCompute'
  import httpClient from '@/utils/httpClient';
  export default {
    data() {
      return {
        options: [],
        selectedOptions: [],
        list: []
      };
    },
    props: {
      addrParam: {
        default: ""
      },
      disabled:{
        default:false
      },
      countryCode:{
        default:""
      }
    },
    ready(){

    },
    watch: {
      'addrParam': function (n, o) {
        this.recursive(n)
      },
      'countryCode': function (n, o) {
        this.options=[];
        this.init();
      }
    },
    mounted(){
      this.$nextTick(function() {
        if(this.countryCode){
          this.init()
        }
      })
    },
    methods: {
      handleChange(value) {
        this.$emit('addrChange', this.selectedOptions);
      },
      init(){
        let data={
          position:"financialInstitution_getAdminDivisionItem",
          paramMap:{
            country_code:this.countryCode
          }
        };
        let _this=this;
        httpClient(querySql,data,function(error, results) {
          console.log(results)
          for(let i=0;i<results.length;i++){
            let param={
              parentId: results[i].parent_id,
              id: results[i].id,
              value: results[i].id,
              label: results[i].name,
            }
            _this.list.push(param);
            _this.options = list2tree(_this.list);
          }
        });
      },
      /**
       * 递归查询地址
       *
       */
      recursive(id){
        let _this=this;
        let data={
          id:id
        }
        if(!id){
          _this.selectedOptions = [];
          return;
        }
        httpClient(getAdi,data,function (error, results) {
          if(results){
            for (let i=0;i<results.length;i++){
              _this.selectedOptions.push(results[i].id)
            }
          }
        })
      }
    },
  };
</script>
<style>
  .el-cascader--small .el-cascader__label{
    line-height:36px;
  }
</style>
